<template>
    <div>
        <el-card class="!border-none" shadow="never">
            <div>
                <el-button
                    v-perms="['tag/add']"
                    type="primary"
                    class="mb-4"
                    @click="handleAdd()"
                >
                    <template #icon>
                        <icon name="el-icon-Plus" />
                    </template>
                    <span>新增标签</span>
                </el-button>
            </div>

            <el-table size="large" v-loading="pager.loading" :data="pager.lists">
                <el-table-column label="序号(ID)" prop="id" min-width="80" />
                <el-table-column label="标签名称" prop="name" min-width="160" />
                <el-table-column label="所属产品分类" min-width="160">
                    <template #default="{ row }">
                        {{ localGetProductCateText(row.product_cate_arr) }}
                    </template>
                </el-table-column>
                <el-table-column label="备注" prop="remark" min-width="160" />
                <el-table-column label="启用状态" min-width="160">
                    <template #default="{ row }">
                        <span v-if="row.is_enable == 1">启用</span>
                        <span v-if="row.is_enable == 0" style="color: red">停用</span>
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="160" fixed="right">
                    <template #default="{ row }">
                        <el-button
                            v-perms="['tag/edit']"
                            type="primary"
                            link
                            @click="handleEdit(row)"
                            >编辑</el-button
                        >
                        <el-button
                            v-perms="['tag/delete']"
                            type="danger"
                            link
                            @click="handleDelete(row.id,row.area_id)"
                            >删除</el-button
                        >
                        <el-switch
                            v-perms="['tag/updateStatus']"
                            v-model="row.is_enable"
                            :active-value="1"
                            :inactive-value="0"
                            @change="changeStatus($event, row.id, row.area_id)"
                        />
                    </template>
                </el-table-column>
            </el-table>
            <div class="flex justify-end mt-4">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>

        <edit-popup v-if="showEdit" ref="editRef" @success="getLists" @close="showEdit = false" />
    </div>
</template>

<script lang="ts" setup name="channelLabel">
import { usePaging } from '@/hooks/usePaging'
import feedback from '@/utils/feedback'
import { tagLists, tagDelete, tagStatus } from '@/api/channel/tag'
import EditPopup from './edit.vue'
import { shallowRef } from 'vue'
const editRef = shallowRef<InstanceType<typeof EditPopup>>()
const showEdit = ref(false)
const handleAdd = async (id?: number) => {
    showEdit.value = true
    await nextTick()
    if (id) {
        editRef.value?.setFormData({
            pid: id
        })
    }
    editRef.value?.open('add')
}

const queryParams = reactive({})
const { pager, getLists, resetPage, resetParams } = usePaging({
    fetchFun: tagLists,
    params: queryParams
})

const handleEdit = async (data: any) => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('edit')
    editRef.value?.getDetail(data)
}

const handleDelete = async (id: number, area_id: String) => {
    await feedback.confirm('确定删除该标签？')
    await tagDelete({ id, area_id})
    getLists()
}

const changeStatus = async (is_enable: any, id: number, area_id: String) => {
    await feedback
        .confirm(
            is_enable == 0
                ? '停用之后，渠道将不能看使用该标签，是否确认停用？'
                : is_enable == 1
                ? '启用之后，渠道将能使用该标签，是否确认启用？'
                : ''
        )
        .then(async () => {
            try {
                await tagStatus({ id, is_enable, area_id })
                getLists()
            } catch (error) {
                getLists()
            }
        })
        .catch(() => {
            getLists()
        })
}

const localGetProductCateText = (productCateArr: Array<any>): string => {
  let productCateText = ''
  productCateArr.forEach((item, index) => {
    productCateText += (item?.name)+'/'
  })
  return productCateText.replace(/[\\//]$/, "");
}

onActivated(() => {
    getLists()
})

getLists()
</script>

<style scoped lang="scss"></style>
